package com.zjitc.utils.redis;

import com.google.gson.Gson;
import com.zjitc.dao.*;
import com.zjitc.model.*;
import com.zjitc.service.ShippingService;

import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author Administrator
 */
@Component
public class MyRedis implements RedisCommand {

    private JedisPool pool;

    @Autowired
    public MyRedis(
            JedisPool pool,
            UserDao dao,
            ProductDao productDao,
            OrderDao orderDao,
            OrderItemDao orderItemDao,
            CategoryDao categoryDao,
            CarDao carDao,
            ShippingDao shippingDao) {
        this.pool = pool;
        /**
         *自动注入全部的users
         */
        Map<String, String> map = new HashMap<String, String>();
        List<User> users = dao.findAll();
        Gson gson = new Gson();
        for (User user : users) {
            map.put(user.getUid(), gson.toJson(user));
        }
        /**
         * 自动注入全部的products
         */
        Map<String, String> productMap = new HashMap<>();
        List<Product> products = productDao.findProducts();
        for (Product product : products) {
            productMap.put(product.getPid(), gson.toJson(product));
        }

        Map<String, String> orderMap = new HashMap<>();
        List<Order> orderList = orderDao.findOrders();
        for (Order order : orderList) {
            orderMap.put(order.getOid(), gson.toJson(order));
        }
        Map<String, String> orderItemMap = new HashMap<>();
        List<OrderItem> orderItemList = orderItemDao.findOrderItems();
        for (OrderItem orderItem : orderItemList) {
            orderItemMap.put(orderItem.getItemId(), gson.toJson(orderItem));
        }
        Map<String, String> categoryMap = new HashMap<>();
        List<Category> categoryList = categoryDao.findAll();
        for (Category category : categoryList) {
            categoryMap.put(category.getCid(), gson.toJson(category));
        }

        Map<String, String> shippingMap = new HashMap<>();
        List<Shipping> shippingList = shippingDao.findAll();
        for (Shipping shipping : shippingList) {
            shippingMap.put(shipping.getUid(), gson.toJson(shipping));
        }

        /**
         * 缓存分页
         */
        /**
         * 如果从数据库查询的数据为空，，，，缓存是添加不进去的
         */
        try (Jedis jedis = pool.getResource()) {
            jedis.hmset("users", map);
            jedis.hmset("products", productMap);
            jedis.hmset("orders", orderMap);
            jedis.hmset("orderItems", orderItemMap);
            jedis.hmset("categories", categoryMap);
            jedis.hmset("HarvestAddresses", shippingMap);
        }
    }

    @Override
    public boolean exists(String key) {
        try (Jedis jedis = pool.getResource()) {
            boolean s = jedis.exists(key);
            return s;
        }
    }

    /**
     * 获取某一个字段的缓存对象
     */
    @Override
    public String getOneById(String redisKey, String filed) {
        try (Jedis jedis = pool.getResource()) {
            String s = jedis.hget(redisKey, filed);
            return s;
        }
    }

    /**
     * 删除某一个字段的缓存
     */
    @Override
    public void deleteById(String redisKey, String filed) {
        try (Jedis jedis = pool.getResource()) {
            jedis.hdel(redisKey, filed);
        }
    }

    /**
     * 修改某个字段的缓存
     */
    @Override
    public void alertById(String redisKey, String filed, String value) {
        try (Jedis jedis = pool.getResource()) {
            jedis.hset(redisKey, filed, value);
        }
    }

    /**
     * 添加一个缓存
     */
    @Override
    public void addCathc(String redisKey, String filed, String value) {

        try (Jedis jedis = pool.getResource()) {
            jedis.hsetnx(redisKey, filed, value);
        }
    }

    /**
     * 是否存在某个字段（通过id进行判断是否存在）
     */
    @Override
    public boolean hexists(String redisKey, String filed) {
        try (Jedis jedis = pool.getResource()) {
            return jedis.hexists(redisKey, filed);
        }
    }


    private Map<String, String> hgetAll(String redisKey) {
        try (Jedis jedis = pool.getResource()) {
            return jedis.hgetAll(redisKey);
        }
    }

    @Override
    public List<User> allUsers() {
        List<User> list = new ArrayList<>();
        Map<String, String> map = hgetAll("users");
        for (Map.Entry<String, String> entry : map.entrySet()) {
            String value = entry.getValue().toString();
            Gson gson = new Gson();
            User user = gson.fromJson(value, User.class);
            list.add(user);
        }
        System.out.println(list);
        return list;
    }

    @Override
    public List<Product> allProducts() {
        List<Product> list = new ArrayList<>();
        Map<String, String> map = hgetAll("products");
        for (Map.Entry<String, String> entry : map.entrySet()) {
            String value = entry.getValue().toString();
            Gson gson = new Gson();
            Product product = gson.fromJson(value, Product.class);
            list.add(product);
        }
        return list;
    }

    @Override
    public List<Order> allOrders() {
        List<Order> list = new ArrayList<>();
        Map<String, String> map = hgetAll("orders");

        for (Map.Entry<String, String> entry : map.entrySet()) {
            String value = entry.getValue().toString();
            Gson gson = new Gson();
            Order order = gson.fromJson(value, Order.class);
            list.add(order);
        }
        return list;
    }

    @Override
    public List<OrderItem> allItems() {
        List<OrderItem> list = new ArrayList<>();
        Map<String, String> map = hgetAll("orderItems");

        for (Map.Entry<String, String> entry : map.entrySet()) {
            String value = entry.getValue().toString();
            Gson gson = new Gson();
            OrderItem orderItem = gson.fromJson(value, OrderItem.class);
            list.add(orderItem);
        }
        return list;
    }

    @Override
    public List<Category> allCategories() {
        List<Category> list = new ArrayList<>();
        Map<String, String> map = hgetAll("categories");

        for (Map.Entry<String, String> entry : map.entrySet()) {
            String value = entry.getValue().toString();
            Gson gson = new Gson();
            Category category = gson.fromJson(value, Category.class);
            list.add(category);
        }
        return list;
    }

    @Override
    public Set<String> zrange(String key, int start, int end) {
        try (Jedis jedis = pool.getResource()) {
            return jedis.zrange(key, start, end);
        }
    }

    @Override
    public Long zcard(String pageCidKey) {
        try (Jedis jedis = pool.getResource()) {
            return jedis.zcard(pageCidKey);
        }
    }

    @Override
    public void del(String key) {
        try (Jedis jedis = pool.getResource()) {
            jedis.del(key);
        }
    }

    @Override
    public List<String> hvals(String key) {
        try (Jedis jedis = pool.getResource()) {
            return jedis.hvals(key);
        }
    }

    @Override
    public void zadd(String key, double score, String value) {
        try (Jedis jedis = pool.getResource()) {
            jedis.zadd(key, score, value);
        }
    }

    @Override
    public void sadd(String key, String value) {
        try (Jedis jedis = pool.getResource()) {
            jedis.sadd(key, value);
        }
    }

    @Override
    public Set<String> smembers(String key) {
        try (Jedis jedis = pool.getResource()) {
            return jedis.smembers(key);
        }
    }

    @Override
    public Set<String> keys(String s) {
        try (Jedis jedis = pool.getResource()) {
            return jedis.keys(s);
        }
    }
}
